#ifndef SRC_BASE_LISTENER_H_
#define SRC_BASE_LISTENER_H_

#include "base/event_loop.h"
#include "base/sigslot.h"
#include "event2/listener.h"

class Listener : public noncopyable,
  public std::enable_shared_from_this<Listener> {
 public:
  typedef std::shared_ptr<Listener> Ptr;
  explicit Listener(EventLoop::Ptr event_loop);
  virtual ~Listener();

 public:
  sigslot::signal4<Listener::Ptr,
          evutil_socket_t,
          struct sockaddr *,
            int> SignalAccept;
  sigslot::signal1<Listener::Ptr> SignalError;
 public:
  bool Start(const std::string &ip, int port);

  void OnAccept(struct evconnlistener *listener,
                evutil_socket_t fd,
                struct sockaddr *saddr,
                int slen);
  void OnError(struct evconnlistener *listener);

 private:
  EventLoop::Ptr event_loop_;
  struct evconnlistener *listener_;
};

#endif  // SRC_BASE_LISTENER_H_



